layui.config({
    base: "static/layui/"
}).extend({
    tableSelect: 'tableSelect'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload','tableSelect'], function () {
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    var tableSelect = layui.tableSelect;

    form.render();

    var myDate = new Date();//获取当前时间设置选择时间的范围
    var maxtime = myDate.getFullYear() + "-" + (myDate.getMonth() + 1).toString().padStart(2, '0') + "-" + (myDate.getDate()).toString().padStart(2, '0');

    laydate.render({
        elem: '#startTime'
        ,max: maxtime
        ,format: 'yyyy-MM-dd'
        ,value: maxtime
    });

    laydate.render({
        elem: '#endTime'
        ,max: maxtime
        ,format: 'yyyy-MM-dd'
        ,value: maxtime
    });

    tableSelect.render({
        elem: '#cusCode',
        checkedKey: 'cusCode',
        searchPlaceholder: '企业编码',
        searchKey: 'cusCode',
        table: {
            url: 'manage/common/list/customer'
            ,method: 'post'
            ,cols: [
                [
                    { type: 'radio' },
                    { field: 'cusCode', title: '企业编码' },
                    { field: 'cusName', title: '企业名称' },
                ]
            ]
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.cusCode)
            })
            elem.val(NEWJSON.join(","))
        }
    })

    table.render({
        elem: '#table'
        ,title: '列表'
        ,method: 'post'
        ,height: 'full-100'
        ,page: true //开启分页
        ,limit: 20
        ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [
            [
                { field: 'lngWorkLoadNo', title: '⼯单号' },
                { field: 'times', title: '预计起始⽣产⽇期' },
                { field: 'material', title: '⼚商料号' },
                { field: 'strDepartmentName', title: '纬创料号或制程' },
                { field: 'strGoodsName', title: '品名或料号描述' },
                { field: 'planSum', title: '计划数' },
                { field: 'invest', title: '投入数'},
                { field: 'pending', title: '待处理数' },
                { field: 'pendingCumulative', title: '累计待处理数' },
                { field: 'nonDefective', title: '良品数' },
                { field: 'defectiveProducts', title: '不良品数' },
                { field: 'poorDistribution', title: '不良分布'},
                { field: 'percentage', title: '良率',
                    templet: function (d) {
                        return d.percentage + '%'
                    }
                }
            ]
        ]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,data: []
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
    });

    table.on('toolbar(table)', function(obj){
        switch (obj.event) {
            case 'find':
                if($("#startTime").val() == ''){
                    layer.msg("请选择开始时间", {icon: 2});
                    return;
                }
                if($("#endTime").val() == ''){
                    layer.msg("请选择结束时间", {icon: 2});
                    return;
                }
                if($('#cusCode').val() == ''){
                    layer.msg("请选择供应商", {icon: 2});
                    return;
                }
                table.reload('table', {
                    url: '/wc_api/qualityData'
                    ,where: {
                        cusCode:$('#cusCode').val(),
                        startTime:$('#startTime').val(),
                        endTime:$('#endTime').val(),
                        material:$('#material').val()
                    } //设定异步数据接口的额外参数
                    ,page: {
                        curr: 1
                    }
                });
                break;
            case 'download':
                if($("#startTime").val() == ''){
                    layer.msg("请选择开始时间", {icon: 2});
                    return;
                }
                if($("#endTime").val() == ''){
                    layer.msg("请选择结束时间", {icon: 2});
                    return;
                }
                if($('#cusCode').val() == ''){
                    layer.msg("请选择供应商", {icon: 2});
                    return;
                }
                let cusCode = $('#cusCode').val();
                let startTime = $('#startTime').val();
                let endTime = $('#endTime').val();
                let material = $('#material').val();
                layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                    window.location.href = "/wc_api/qualityData/download?str=前&cusCode=" + cusCode + "&startTime=" + startTime + "&endTime=" + endTime+"&material=" + material;
                    layer.close(index);
                });
                break;
        }
    })

})